package com.nsoft.thickclient.trackMove.gc;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

import com.nsoft.thickclient.components.VoPropertyChooserField;
import com.nsoft.thickclient.components.VoPropertyNumaricField;
import com.nsoft.thickclient.components.VoPropertySearchField;
import com.nsoft.thickclient.components.VoPropertySearchFieldAction;
import com.nsoft.thickclient.components.VoPropertyTextArea;
import com.nsoft.thickclient.containers.NBoxPanel;
import com.nsoft.thickclient.containers.NGridPanel;
import com.nsoft.thickclient.containers.VOPropertyComponentPanel;
import com.nsoft.thickclient.controller.ScreenView;
import com.nsoft.thickclient.model.VoModel;
import com.nsoft.thickclient.trackMove.intr.gc.GcGeneral;
import com.nsoft.thickclient.trackMove.intr.gc.GcStatus;
import com.nsoft.thickclient.trackMove.intr.party.ThinParty;

public class GcGeneralView extends ScreenView<GcGeneral> {

	/**
	 * serialVersionUID = 1L.
	 */
	private static final long serialVersionUID = 1L;

	private boolean editMode;

	private VoModel model;

	private String[] status = { "open - Open", "submit - Submitted",
			"delivered - Delivered", "billed - Billed", "paid - Paid" };

	public GcGeneralView(GcGeneral bean) {
		if (bean != null) {
			model = new VoModel(bean);
			editMode = true;
		} else {
			model = new VoModel(GcGeneral.class);
			editMode = false;
		}
		NBoxPanel boxPanel = new NBoxPanel(NBoxPanel.VERTICAL);
		boxPanel.addComponent(getDetailsPanel());
		boxPanel.addComponent(getAddressPanel());
		boxPanel.addComponent(getChargesPanel());
		// boxPanel.addComponent(getNotesPanel());
		this.add(boxPanel);
	}

	private JComponent getDetailsPanel() {
		JPanel detailsPanel = new JPanel(new BorderLayout());
		JPanel topPanel = new JPanel(new GridLayout(1, 2));
		VOPropertyComponentPanel leftPanel = new VOPropertyComponentPanel();
		leftPanel.addVoTextField(model, "gcNo", "Gc Nunber", false);
		VoPropertySearchField lorrySearchField = new VoPropertySearchField(
				model, "lorryNo", "id", new VoPropertySearchFieldAction() {

					public List<?> getData(String filter) {
						// TODO Srvice call.
						List<ThinParty> list2 = new ArrayList<ThinParty>();
						list2.add(new ThinParty("Laxmi", 1l, "Test Data"));
						return list2;
					}

					public List<?> getDataByMappedavalue(Object value) {
						// TODO Auto-generated method stub
						return null;
					}

				});
		leftPanel.addComponent("Vehicle Number", lorrySearchField);
		//leftPanel.addVoTextField(model, "lorryNo", "Vehicle Number", true);
		leftPanel.addVoDateChooser(model, "dispatchedDate", "Dispatched Date",
				true, true);
		VOPropertyComponentPanel rightPanel = new VOPropertyComponentPanel();
		List<GcStatus> statusList = new ArrayList<GcStatus>();
		for (long i = 0l; i < status.length; i++) {
			statusList.add(new GcStatus(i, status[(int) i]));
		}
		VoPropertyChooserField statusField = new VoPropertyChooserField(model,
				"status", "id", "description", statusList);
		rightPanel.addComponent("Status", statusField);
		VoPropertySearchField transporterSearchField = new VoPropertySearchField(
				model, "transportId", "id", new VoPropertySearchFieldAction() {

					public List<?> getData(String filter) {
						// TODO Srvice call.
						List<ThinParty> list2 = new ArrayList<ThinParty>();
						list2.add(new ThinParty("Laxmi", 1l, "Test Data"));
						return list2;
					}

					public List<?> getDataByMappedavalue(Object value) {
						// TODO Auto-generated method stub
						return null;
					}

				});
		rightPanel.addComponent("Transporter Name", transporterSearchField);
		rightPanel.addVoDateChooser(model, "deliveredDate", "Delivered Date",
				true, true);
		topPanel.add(leftPanel);
		topPanel.add(rightPanel);
		detailsPanel.add(topPanel, BorderLayout.NORTH);
		return detailsPanel;
	}

	private JComponent getAddressPanel() {
		JPanel addressPanel = new JPanel(new GridLayout(1, 2));
		VOPropertyComponentPanel consignorPanel = new VOPropertyComponentPanel();
		final VoPropertySearchField consignorPartySearchField = new VoPropertySearchField(
				model, "consignorId", "id", new VoPropertySearchFieldAction() {

					public List<?> getData(String filter) {
						// TODO Srvice call.
						List<ThinParty> list2 = new ArrayList<ThinParty>();
						list2.add(new ThinParty("Laxmi", 1l, "Test Data"));
						return list2;
					}

					public List<?> getDataByMappedavalue(Object value) {
						// TODO Auto-generated method stub
						return null;
					}

				});
		consignorPanel.addComponent("Name", consignorPartySearchField);
		final VoPropertyTextArea consignorAddressField = new VoPropertyTextArea(
				model, "consignorAddress");
		consignorAddressField.setPreferredSize(new Dimension(200, 80));
		consignorPartySearchField.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				consignorAddressField
						.setText(((ThinParty) consignorPartySearchField
								.getSelectedItem()).getAddress());

			}

		});
		consignorPanel.addComponent("Address", consignorAddressField);
		consignorPanel.setBorder(BorderFactory
				.createTitledBorder("Consignor details"));
		addressPanel.add(consignorPanel);
		/** ********************************** */
		VOPropertyComponentPanel consigneePanel = new VOPropertyComponentPanel();
		final VoPropertySearchField consigneePartySearchField = new VoPropertySearchField(
				model, "consigneeId", "id", new VoPropertySearchFieldAction() {

					public List<?> getData(String filter) {
						// TODO Srvice call.
						List<ThinParty> list2 = new ArrayList<ThinParty>();
						list2.add(new ThinParty("Laxmi", 1l, "Test Data"));
						return list2;
					}

					public List<?> getDataByMappedavalue(Object value) {
						// TODO Auto-generated method stub
						return null;
					}

				});
		consigneePanel.addComponent("Name", consigneePartySearchField);
		final VoPropertyTextArea consigneeAddressField = new VoPropertyTextArea(
				model, "consigneeAddress");
		consigneeAddressField.setPreferredSize(new Dimension(200, 80));
		consigneePartySearchField.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				consigneeAddressField
						.setText(((ThinParty) consigneePartySearchField
								.getSelectedItem()).getAddress());

			}

		});
		consigneePanel.addComponent("Address", consigneeAddressField);
		consigneePanel.setBorder(BorderFactory
				.createTitledBorder("Consignee details"));
		addressPanel.add(consigneePanel);
		return addressPanel;
	}

	private JComponent getChargesPanel() {
		NGridPanel chargesPanel = new NGridPanel();
		chargesPanel.setBorder(BorderFactory.createTitledBorder("Charges"));
		chargesPanel.addComponent("Freight", getNumberField("freight", true),
				0, 0);
		chargesPanel.addComponent("S-Charge", getNumberField("charge1", true),
				1, 0);
		chargesPanel.addComponent("Hamali", getNumberField("charge2", true), 2,
				0);

		chargesPanel.addComponent("Risk charge",
				getNumberField("charge3", true), 0, 1);
		chargesPanel.addComponent("Other charge-1", getNumberField("charge4",
				true), 1, 1);
		chargesPanel.addComponent("Other charge-2", getNumberField("charge5",
				true), 2, 1);

		chargesPanel.addComponent("Total charger", getNumberField(
				"totalCharges", false), 0, 2);
		chargesPanel.addComponent("Paid", getNumberField("paid", true), 1, 2);
		chargesPanel.addComponent("Balance", getNumberField("balance", false),
				2, 2);
		return chargesPanel;
	}

	private VoPropertyNumaricField getNumberField(String prop, boolean editable) {
		VoPropertyNumaricField field = new VoPropertyNumaricField(model, prop,
				true);
		field.setEnabled(editable);
		//field.setBackground(Color.WHITE);
		return field;
	}

	@Override
	public GcGeneral getWorkingBean() {
		return (GcGeneral) model.getValueObject();
	}

	@Override
	public void setWorkingBean(GcGeneral bean) {
		model.setValueObject(bean);
	}

	public VoModel getModel() {
		return model;
	}

	public void setModel(VoModel model) {
		this.model = model;
	}

}
